package com.xuecheng.web.exception;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义异常处理器
 * 1.在类上声明异常处理器
 * 2.定义异常处理的方法并在方法上指定，可处理的异常类型
 *            方法：
 *                  返回值和controller一致
 *                  参数：异常对象
 */
@RestControllerAdvice(
        basePackages = {
                "com.xuecheng.system.controller",
                "com.xuecheng.content.controller",
                "com.xuecheng.learning.controller",
                "com.xuecheng.order.controller",
                "com.xuecheng.search.controller",
                "com.xuecheng.media.controller",
                "com.xuecheng.basic.controller"
        }
)
@Slf4j
public class GlobalExceptionHandler {

    //处理公共的异常
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseResult handlerRuntimeException (RuntimeException e) {
        log.error(e.getMessage(),e);
        return ResponseResult.errorResult(ErrorCode.ERROR);
    }

    //处理自定义异常
    @ExceptionHandler(value = BusinessException.class)
    public ResponseResult handlerBusinessException (BusinessException e) {
        ErrorCode code = e.getCode();
        return ResponseResult.errorResult(code);
    }
}
